/**
 * 110. 平衡二叉树
 */
public class No110 {
    public boolean isBalanced(TreeNode root) {
        return deep(root) != -1;
    }

    public int deep(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int deepLeft = deep(root.left);
        if (deepLeft == -1) {
            return -1;
        }
        int deepRight = deep(root.right);
        if (deepRight == -1) {
            return -1;
        }

        if (Math.abs(deep(root.left) - deep(root.right)) > 1) {
            return -1;
        }

        return Math.max(deepLeft, deepRight) + 1;
    }
}
